import { BadGatewayException, Injectable } from '@nestjs/common';
import { ModelType } from '@typegoose/typegoose/lib/types';
import * as crypto from 'crypto'
import { InjectModel } from 'nestjs-typegoose';
import { JihuoModel } from 'src/mongodb/models/jihuoModel/jihuoModel';
import { QudaobaoModel } from 'src/mongodb/models/qudaobaoModel/qudaobaoModel';
import { OperatService } from '../../operatApi/operat.service';

@Injectable()
export class ZhuceService {


    constructor(
        @InjectModel(JihuoModel) private readonly jihuo: ModelType<JihuoModel>,
        @InjectModel(QudaobaoModel) private readonly qudaobao: ModelType<QudaobaoModel>,
        private readonly operat: OperatService,
    ) { }

    /* 点击注册 */
    async clickZhuc(body, req, channelResult, ip) {
        try {
            if (body.imei) {
                const md5 = crypto.createHash('md5');
                const imeimd5 = md5.update(body.imei).digest('hex')
                try {
                    /* web模式添加ip信息 */
                    if (channelResult.webmodel) {
                        let parse = JSON.parse(body.imei)
                        parse.ips = ip
                        body.imei = JSON.stringify(parse)
                    }
                    /* 是否加白 */
                    if (!channelResult.whitelist) {
                        const zhucheData = await this.jihuo.findOne({ ditch: req.dns, imeiMd5: imeimd5, type: 'zhuche' })

                        if (!zhucheData) {
                            /* 统计渠道包次数 */
                            await this.qudaobao.updateOne({ mark: req.dns }, { $inc: { todayzhuce: 1, totalzhuce: 1 } })
                            /* 存用户设备信息 */
                            await this.jihuo.create({
                                ditch: req.dns,
                                imeiMd5: imeimd5,
                                imei: body.imei,
                                visitorid: body.visitorId,
                                platform: body.platform,
                                createtime: Math.floor(Date.now() / 1000),
                                type: 'zhuche',
                                userid: channelResult.userid,
                                username: channelResult.username
                            })
                            await this.operat.methodFun(body, req, 'zhuche')
                        } else {
                            console.log('设备重复注册');
                        }
                    } else {
                        await this.operat.methodFun(body, req, 'zhuche')
                        console.log('注册加白联调');
                    }
                } catch (error) {
                    console.log(error);
                    throw error
                }
            } else {
                /* 判断浏览器是否访问联系方式 */
                if (process.env.WEB != 'true') {
                    console.log("登录注册异常" + JSON.stringify(body));
                    throw '登录注册手机环境异常,请使用正规手机端打开app哟,或者重启app再试哟'
                }
            }
        } catch (error) {
            console.log(error);
            throw new BadGatewayException(error)
        }
    }
}
